package com.shy.collect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shy.collect.entity.PayProduct;
import com.shy.collect.mapper.PayProductMapper;
import com.shy.collect.service.ProductService;
import com.shy.configs.common.enums.PublicStatus;
import com.shy.configs.common.enums.RCode;
import com.shy.configs.common.exceptions.DftException;
import com.shy.configs.mvc.models.query.ProductQuery;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private PayProductMapper productMapper;

    /**
     * 分页查询产品支付信息
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public IPage<PayProduct> pageList(Integer pageNo, Integer pageSize) {
        QueryWrapper<PayProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("edit_time");
        return productMapper.selectPage(Page.of(pageNo, pageSize), queryWrapper);
    }

    /**
     * 添加产品支付信息
     * @param productQuery
     */
    @Override
    public void addProduct(ProductQuery productQuery) {
        // 校验产品编码是否已存在
        QueryWrapper<PayProduct> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("product_code", productQuery.getProductCode());
        PayProduct payProduct= productMapper.selectOne(queryWrapper1);
        if (payProduct != null) {
            throw new DftException(RCode.PRODUCT_CODE_EXIST);
        }
        // 校验产品名称是否已存在
        QueryWrapper<PayProduct> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("product_name", productQuery.getProductName());
        payProduct= productMapper.selectOne(queryWrapper2);
        if (payProduct != null) {
            throw new DftException(RCode.PRODUCT_NAME_EXIST);
        }
        payProduct = new PayProduct();
        // 设置产品支付信息
        payProduct.setProductCode(productQuery.getProductCode());
        // 设置产品支付信息
        payProduct.setProductName(productQuery.getProductName());
        // 设置产品支付状态
        payProduct.setStatus(PublicStatus.ENABLE.name());
        // 设置产品支付编辑时间
        payProduct.setEditTime(new Date());
        productMapper.insert(payProduct);
    }

    /**
     * 修改产品支付状态
     * @param productCode
     */
    @Override
    public void editStatus(String productCode) {
        //查询当前产品支付状态，并修改为相反值
        QueryWrapper<PayProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_code", productCode);
        PayProduct payProduct = productMapper.selectOne(queryWrapper);
        if (payProduct != null) {
            if (PublicStatus.ENABLE.name().equals(payProduct.getStatus())) {
                payProduct.setStatus(PublicStatus.DISABLE.name());
            } else {
                payProduct.setStatus(PublicStatus.ENABLE.name());
            }
            productMapper.updateById(payProduct);
        }
    }

    @Override
    public List<PayProduct> dicList(String productName) {
        QueryWrapper<PayProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("product_name",productName).orderByAsc("product_name");
        return productMapper.selectList(queryWrapper);
    }

    @Override
    public List<PayProduct> productNameList(String productName) {
        QueryWrapper<PayProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("product_name",productName).orderByAsc("product_name").last("limit 0, 50");
        return productMapper.selectList(queryWrapper);
    }

}
